package com.rpj.controller;

import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rpj.entity.ModulesDto;
import com.rpj.entity.ModulesEntity;
import com.rpj.entity.Result;
import com.rpj.service.ModulesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api("模块的增删改")
public class ModulesController {
@Autowired
public ModulesService modulesservice;

@RequestMapping(value="/api/CreateModule", method = RequestMethod.POST)
@ApiOperation("模块的添加")
public Object saveModules(@ApiParam("模块标题")@RequestParam("name")String name,@ApiParam("父模块id")@RequestParam("parentId")int parentId,@ApiParam("模块对应的路径")@RequestParam("path")String path,@ApiParam("权重整数")@RequestParam("weight")int weight,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	ModulesEntity m = new ModulesEntity();
	m.setTitle(name);
	m.setPid(parentId);
	m.setWeigth(weight);
	int i = modulesservice.saveModules(m);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","添加成功",true);
		 }else{
				return new Result("","添加失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}

/**
 *修改用户
 */
@RequestMapping(value="/api/UpdateModule", method = RequestMethod.POST)
@ApiOperation("模块的更新")
public Object updateModules(@ApiParam("模块Id")@RequestParam("mId")int mId,@ApiParam("模块标题")@RequestParam("name")String name,@ApiParam("父模块id")@RequestParam("parentId")int parentId,@ApiParam("模块对应的路径")@RequestParam("path")String path,@ApiParam("权重整数")@RequestParam("weight")String weight,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	ModulesEntity m = new ModulesEntity();
	m.setTitle(name);
	m.setPid(parentId);
	m.setId(mId);
	int i = modulesservice.updataModules(m);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","修改成功",true);
		 }else{
				return new Result("","修改失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
/**
 * 删除模块
 */
@RequestMapping(value="/api/DeleteModule", method = RequestMethod.POST)
@ApiOperation("模块的删除")
public Object delModules(@ApiParam("模块Id")@RequestParam("mId")int mId,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	ModulesEntity m = new ModulesEntity();
	m.setId(mId);
	int i = modulesservice.delModules(m);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","删除成功",true);
		 }else{
				return new Result("","删除失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
/*
 * 查询/api/GetAllModuleLayUI
 */
@RequestMapping(value="/api/GetAllModuleLayUI", method = RequestMethod.POST)
@ApiOperation("模块的查询")
public Object findModules(HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {

	 List<ModulesDto> list = modulesservice.loadTree();
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(list!=null) {
				return new Result("","成功",true);
		 }else{
				return new Result("","失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
/**
 * 获取权限
 */
@ApiOperation("根据角色获取权限功能")
@PostMapping("/api/GetSysRightsJsonLayUI")
public Object getSysRightLayui(@ApiParam("数据库秘钥") @RequestParam("token")String token,HttpSession session,
		@ApiParam("角色名称")@RequestParam("roleNames")List<String> list) {
	String getToken =(String) session.getAttribute("token");
    if(getToken.equals(token)) {
  	  return modulesservice.getsysRight(list);
    }else {
  	  Result result = new Result("","token错误",false);
  	  return result;
    }
}
}
